<?php
require_once 'init_app.php';

set_time_limit(0); 

// Может переделать - для всех sort от 0 до 100к, у популярных товаров наугад от 100к до 1кк??

$table_name = 'products';
$max_sort   = 100000;

// перемешать все рандомно
$offset = 0;
$limit  = 1000;
/*do {
    usleep(500000);
    $products = DB::select()
                    ->from($table_name)
                    ->where('is_available', '=', 1)
                    ->where('shop_id', '=', 4965)
                    ->offset($offset)
                    ->limit($limit)  
                    ->execute()
                    ->as_array();  
    $offset += $limit;
    
    $ids = array();
    foreach ($products as $product) {
        $ids[] = $product['id'];    
    }
    
    if (count($ids)) {
        DB::update($table_name)
            ->set(array('sort' => DB::expr('FLOOR( 1 + RAND( ) * '.$max_sort.')')))
            ->where('id', 'IN', $ids)
            ->execute();     
    }                
    
} while (count($products));  */  

// приплюснуть к sort число, прямопроп. числу заказов (для али, вб)
// - али, 6115
$shop_id = 6115;
/*$stat = DB::select(array(DB::expr('MAX(orders_count)'), 'max'), array(DB::expr('MIN(orders_count)'), 'min'))
                    ->from($table_name)
                    ->where('shop_id', '=', $shop_id) 
                    ->limit(1)        
                    ->execute()
                    ->as_array();   */
$max    = 3000;     //$stat[0]['max'];          // нужно для нормализации кол-ва заказов
$min    = 0;        //$stat[0]['min'];
$medium = 500;      //round(($max + $min)/2);   // будем обрабатывать только самые покупаемы товары, из-за большой нагрузки

$offset = 0;
$limit  = 1000;
/*do {
    $products = DB::select()
                    ->from($table_name)
                    ->where('is_available', '=', 1)
                    ->where('orders_count', '>', $medium)
                    ->where('shop_id', '=', $shop_id)
                    ->offset($offset)
                    ->limit($limit)  
                    ->execute()
                    ->as_array(); 
    $offset += $limit;
    
    foreach ($products as $product) {
        $orders_count     = $product['orders_count'];
        $normalized_value = ($orders_count - $min)/($max - $min);   // (val - min) / (max - min)
        if ($normalized_value > 1) {
            $normalized_value = 1;    
        }
        $add_sort         = $normalized_value * $max_sort;
        $add_sort         = (int)$add_sort;
        
        //echo 'max - '.$max."\n";
        //echo 'min - '.$min."\n";
        //echo 'orders_count - '.$orders_count."\n";
        //echo 'normalized_value - '.$normalized_value."\n";
        //echo 'add_sort - '.$add_sort."\n";
        
        DB::update($table_name)
            ->set(array('sort' => DB::expr('sort + '.$add_sort)))
            ->where('id', '=', $product['id'])
            ->execute();  
    }            
    
} while (count($products));  */  

                    
// - вб, 4102
$shop_id = 4102;
$stat = DB::select(array(DB::expr('MAX(orders_count)'), 'max'), array(DB::expr('MIN(orders_count)'), 'min'))
                    ->from($table_name)
                    ->where('shop_id', '=', $shop_id) 
                    ->limit(1)        
                    ->execute()
                    ->as_array();
$max    = $stat[0]['max'];          // нужно для нормализации кол-ва заказов
$min    = $stat[0]['min'];

$offset = 0;
$limit  = 1000;
/*do {
    $products = DB::select()
                    ->from($table_name)
                    ->where('is_available', '=', 1)
                    ->where('orders_count', '>', 0) 
                    ->where('shop_id', '=', $shop_id)
                    ->offset($offset)
                    ->limit($limit)  
                    ->execute()
                    ->as_array();  
    $offset += $limit;
    
    foreach ($products as $product) {
        $orders_count     = $product['orders_count'];
        $normalized_value = ($orders_count - $min)/($max - $min);   // (val - min) / (max - min)
        $add_sort         = $normalized_value * $max_sort;
        $add_sort         = (int)$add_sort;
        
        //echo 'max - '.$max."\n";
        //echo 'min - '.$min."\n";
        //echo 'orders_count - '.$orders_count."\n";
        //echo 'normalized_value - '.$normalized_value."\n";
        //echo 'add_sort - '.$add_sort."\n";
        
        DB::update($table_name)
            ->set(array('sort' => DB::expr('sort + '.$add_sort)))
            ->where('id', '=', $product['id'])
            ->execute();  
    }            
    
} while (count($products));  */   


// - ламода, 1001
$shop_id = 1001;
$stat = DB::select(array(DB::expr('MAX(orders_count)'), 'max'), array(DB::expr('MIN(orders_count)'), 'min'))
                    ->from($table_name)
                    ->where('shop_id', '=', $shop_id) 
                    ->limit(1)        
                    ->execute()
                    ->as_array();
$max    = $stat[0]['max'];          // нужно для нормализации кол-ва заказов
$min    = $stat[0]['min'];

$offset = 0;
$limit  = 1000;
/*do {
    $products = DB::select()
                    ->from($table_name)
                    ->where('is_available', '=', 1)
                    ->where('orders_count', '>', 0) 
                    ->where('shop_id', '=', $shop_id)
                    ->offset($offset)
                    ->limit($limit)  
                    ->execute()
                    ->as_array();  
    $offset += $limit;
    
    foreach ($products as $product) {
        $orders_count     = $product['orders_count'];
        $normalized_value = ($orders_count - $min)/($max - $min);   // (val - min) / (max - min)
        $add_sort         = $normalized_value * $max_sort;
        $add_sort         = (int)$add_sort;
        
        //echo 'max - '.$max."\n";
        //echo 'min - '.$min."\n";
        //echo 'orders_count - '.$orders_count."\n";
        //echo 'normalized_value - '.$normalized_value."\n";
        //echo 'add_sort - '.$add_sort."\n";
        
        DB::update($table_name)
            ->set(array('sort' => DB::expr('sort + '.$add_sort)))
            ->where('id', '=', $product['id'])
            ->execute();  
    }            
    
} while (count($products)); */ 

// - ASOS, 4965
$shop_id = 4965;
$stat = DB::select(array(DB::expr('MAX(orders_count)'), 'max'), array(DB::expr('MIN(orders_count)'), 'min'))
                    ->from($table_name)
                    ->where('shop_id', '=', $shop_id) 
                    ->limit(1)        
                    ->execute()
                    ->as_array();
$max    = $stat[0]['max'];          // нужно для нормализации кол-ва заказов
$min    = $stat[0]['min'];

$offset = 0;
$limit  = 1000;
/*do {
    $products = DB::select()
                    ->from($table_name)
                    ->where('is_available', '=', 1)
                    ->where('orders_count', '>', 0) 
                    ->where('shop_id', '=', $shop_id)
                    ->offset($offset)
                    ->limit($limit)  
                    ->execute()
                    ->as_array();  
    $offset += $limit;
    
    foreach ($products as $product) {
        $orders_count     = $product['orders_count'];
        $normalized_value = ($orders_count - $min)/($max - $min);   // (val - min) / (max - min)
        $add_sort         = $normalized_value * $max_sort;
        $add_sort         = (int)$add_sort;
        
        //echo 'max - '.$max."\n";
        //echo 'min - '.$min."\n";
        //echo 'orders_count - '.$orders_count."\n";
        //echo 'normalized_value - '.$normalized_value."\n";
        //echo 'add_sort - '.$add_sort."\n";
        
        DB::update($table_name)
            ->set(array('sort' => DB::expr('sort + '.$add_sort)))
            ->where('id', '=', $product['id'])
            ->execute();  
    }            
    
} while (count($products));    */

// несколько товаров из ебея загнать в топ